package de.kf.expenses;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import de.kf.expenses.database.DbAdapter;

public class ExpenseDetails extends Activity
{
	private EditText mDateText;
	private EditText mAmountText;
	private EditText mBodyText;
	private Long mRowId;
	private DbAdapter mDbHelper;
	private Spinner mCategory;

	@Override
	protected void onCreate(Bundle bundle)
	{
		super.onCreate(bundle);
		mDbHelper = new DbAdapter(this);
		mDbHelper.open();
		setContentView(R.layout.expense_edit);
		mCategory = (Spinner) findViewById(R.id.category);
		mDateText = (EditText) findViewById(R.id.expense_edit_date);
		mDateText.setOnClickListener(dateClickListener);
		mAmountText = (EditText) findViewById(R.id.expense_edit_amount);
		mAmountText.setOnClickListener(amountClickListener);
		mBodyText = (EditText) findViewById(R.id.expense_edit_purpose);

		Button confirmButton = (Button) findViewById(R.id.expense_edit_button);
		mRowId = null;
		Bundle extras = getIntent().getExtras();
		mRowId = (bundle == null) ? null : (Long) bundle
				.getSerializable(DbAdapter.KEY_ROWID);
		if (extras != null)
		{
			mRowId = extras.getLong(DbAdapter.KEY_ROWID);
		}
		populateFields();
		confirmButton.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View view)
			{
				setResult(RESULT_OK);
				finish();
			}

		});
	}

	private OnClickListener dateClickListener = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{

			mDateText.setText(createDateAsString());

		}
	};

	private OnClickListener amountClickListener = new OnClickListener()
	{

		@Override
		public void onClick(View v)
		{

			mAmountText.setText("");

		}
	};

	private void populateFields()
	{
		if (mRowId != null)
		{
			Cursor todo = mDbHelper.fetchTodo(mRowId);
			startManagingCursor(todo);
			String category = todo.getString(todo
					.getColumnIndexOrThrow(DbAdapter.KEY_DATE));

			for (int i = 0; i < mCategory.getCount(); i++)
			{

				String s = (String) mCategory.getItemAtPosition(i);
				Log.e(null, s + " " + category);
				if (s.equalsIgnoreCase(category))
				{
					mCategory.setSelection(i);
				}
			}

			mDateText.setText(todo.getString(todo
					.getColumnIndexOrThrow(DbAdapter.KEY_DATE)));
			mAmountText.setText(todo.getString(todo
					.getColumnIndexOrThrow(DbAdapter.KEY_AMOUNT)));
			mBodyText.setText(todo.getString(todo
					.getColumnIndexOrThrow(DbAdapter.KEY_PURPOSE)));
		}
	}

	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(DbAdapter.KEY_ROWID, mRowId);
	}

	@Override
	protected void onPause()
	{
		super.onPause();
		saveState();
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		populateFields();
	}

	private void saveState()
	{
		// String category = (String) mCategory.getSelectedItem();
		String date = mDateText.getText().toString();
		String summary = mAmountText.getText().toString();
		String description = mBodyText.getText().toString();

		if (mRowId == null)
		{
			long id = mDbHelper.createTodo(date, summary, description);
			if (id > 0)
			{
				mRowId = id;
			}
		}
		else
		{
			mDbHelper.updateTodo(mRowId, date, summary, description);
		}
	}

	private String createDateAsString()
	{
		DateFormat df = DateFormat.getDateInstance();
		return df.format(new Date());
	}
}